<?php
/* for rewrite or iis rewrite */
if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
	$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
} else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
	$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
}
/* end */

error_reporting(E_ALL^E_WARNING^E_NOTICE);
define('SYS_VERSION', '0.92');
define('SYS_TIMESTART', microtime(true));
define('SYS_REQUEST', isset($_SERVER['REQUEST_URI']));
define('DIR_SEPERATOR', strstr(strtoupper(PHP_OS), 'WIN')?'\\':'/');
define('DIR_ROOT', dirname(__FILE__));
define('DIR_LIBARAY', DIR_ROOT . '/library');
define('DIR_CLASSES', DIR_ROOT . '/classes');
define('DIR_COMPILED', DIR_ROOT . '/compiled');
define('DIR_TEMPLATE', DIR_ROOT . '/template');
define('DIR_FUNCTION', DIR_ROOT . '/function');
define('DIR_CONFIGURE', DIR_ROOT . '/configure');
define('SYS_INIFILE', DIR_ROOT . '/configure/system.ini');
define('SYS_XMLFILE', DIR_ROOT . '/configure/system.xml');
define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT']);
define('WWW_ROOT', dirname(DIR_ROOT));
define('IMG_ROOT', dirname(DIR_ROOT) . '/static');

/* web_root */
$WEB_ROOT = '/'.trim(substr(WWW_ROOT,strlen(DOC_ROOT)),DIR_SEPERATOR);
$WWW_ROOT = str_replace(DIR_SEPERATOR, '/', $WWW_ROOT);
define('WEB_ROOT', $WEB_ROOT=='/' ? '' : $WEB_ROOT);

date_default_timezone_set('Asia/Shanghai');

/* important function */
function __autoload($class_name) {
	$file_name = trim(str_replace('_','/',$class_name),'/').'.class.php';
	$file_path = DIR_LIBARAY. '/' . $file_name;
	if ( file_exists( $file_path ) ) {
		return require_once( $file_path );
	}
	$file_path = DIR_CLASSES. '/' . $file_name;
	if ( file_exists( $file_path ) ) {
		return require_once( $file_path );
	}
	return false;
}

function import($funcpre) {
	$file_path = DIR_FUNCTION. '/' . $funcpre . '.php'; 
	if (file_exists($file_path) ) {
		require_once( $file_path );
	}
}

function script() {
	$request_uri = $_SERVER['REQUEST_URI'];
	$request_uri = preg_replace('#^'.WEB_ROOT.'#', '', $request_uri);
	$query_pos = strpos($request_uri, '?');
	$query_pos = $query_pos ? $query_pos : strlen($request_uri);
	$request_uri = trim(substr($request_uri, 0, $query_pos), '/');
	$request_uri = preg_replace('#(\.php)$#', '', $request_uri);
	$request_uri = $request_uri ? $request_uri : 'index';
	$script_name = WWW_ROOT . '/' . $request_uri . '.php';

	/* parse param */
	$us = explode('/', $request_uri);
	$param = array();
	while(count($us)>1) {
		if (file_exists($script_name)) { break; }
		array_unshift($param, array_pop($us));
		$request_uri = join('/', $us);
		$script_name = WWW_ROOT . '/' . $request_uri . '.php';
	}
	if ( count($us)==1 && !file_exists($script_name) ) {
		$us[] = 'index'; 
		$request_uri = join('/', $us);
		$script_name = WWW_ROOT . '/' . $request_uri . '.php';
	} 
	$_GET['param'] = $param;
	/* parse end */

	if ( !file_exists($script_name) ) {
		Utility::Redirect(WEB_ROOT . '/index.php');
		header('HTTP/1.1 404 Not Found');
		die('<h1>Not Found</h1>');
	}
	return $script_name;
}
/* json */
if (!function_exists('json_encode')){function json_encode($v) {return ZendJsonEncoder::encode($v);}}
if (!function_exists('json_decode')){function json_decode($v,$t) {return ZendJsonDecoder::decode($v,$t);}}
/* end json */


/* gz_handler */
if(SYS_REQUEST){
	ob_start();
//	if(function_exists('gzencode')){ob_start('ob_gzhandler'); }
}
/* end gz */

/* import */
import('template');
import('common');
